//videoStatus: -1（未检测）/ 0（不可用）/ 1（可用）
//audioStatus: -1（未检测）/ 0（不可用）/ 1（可用）

let aiMediaCheck = {
  initSet() {
    localStorage.setItem('videoStatus', -1)
    localStorage.setItem('audioStatus', -1)
  },
  getVideoStatus(params) {
    if (
      window.location.protocol !== 'https:' &&
      window.location.hostname != 'localhost'
    ) {
      app.msg({ type: 'error', content: '当前URL协议不能启用摄像头' })
      return
    }
    let status = localStorage.getItem('videoStatus')

    if (status == -1) {
      this.allowOpenVideo(params.startCallback, params.cancelCallback)
    }
    if (status == 0) {
      this.videoUnusable(params.msg)
    }
    if (status == 1) {
      this.runCallback(params.startCallback)
    }
  },
  getAudioStatus(params) {
    if (
      window.location.protocol !== 'https:' &&
      window.location.hostname != 'localhost'
    ) {
      app.msg({ type: 'error', content: '当前URL协议不能启用麦克风' })
      return
    }
    let status = localStorage.getItem('audioStatus')

    if (status == -1) {
      this.allowOpenAudio(params.startCallback, params.cancelCallback)
    }
    if (status == 0) {
      this.audioUnusable(params.msg)
    }
    if (status == 1) {
      this.runCallback(params.startCallback)
    }
  },
  allowOpenVideo(startCallback, cancelCallback) {
    app.confirm({
      title: '提示',
      content: '该功能需要打开摄像头，是否确认开启？',
      onOk: () => {
        //检测video
        this.runCallback(startCallback)
      },
      onCancel: () => {
        app.msg({ type: 'info', content: '您已取消开启该功能' })
        this.runCallback(cancelCallback)
      }
    })
  },

  allowOpenAudio(startCallback, cancelCallback) {
    app.confirm({
      title: '提示',
      content: '该功能需要打开麦克风，是否确认开启？',
      onOk: () => {
        //检测audio
        this.runCallback(startCallback)
      },
      onCancel: () => {
        app.msg({ type: 'info', content: '您已取消开启该功能' })
        this.runCallback(cancelCallback)
      }
    })
  },

  videoUnusable(msg) {
    localStorage.setItem('videoStatus', 0)
    const h = app.root.$createElement
    app.msg({
      type: 'error',
      content: h('span', null, [
        `未检测到摄像头，无法开启${msg}`,
        h(
          'a',
          {
            attrs: {
              href: 'http://anov.cucloud.cn/anov-doc/guide/videoAudio.html',
              target: '_blank'
            }
          },
          [
            h(
              'i',
              {
                style: {
                  fontSize: '12px',
                  color: '#ccc'
                }
              },
              '[帮助]'
            )
          ]
        )
      ])
    })
    return
  },

  audioUnusable(msg) {
    localStorage.setItem('audioStatus', 0)
    const h = app.root.$createElement
    app.msg({
      type: 'error',
      content: h('span', null, [
        `未检测到麦克风，无法开启${msg}`,
        h(
          'a',
          {
            attrs: {
              href: 'http://anov.cucloud.cn/anov-doc/guide/videoAudio.html',
              target: '_blank'
            }
          },
          [
            h(
              'i',
              {
                style: {
                  fontSize: '12px',
                  color: '#ccc'
                }
              },
              '[帮助]'
            )
          ]
        )
      ])
    })
    return
  },

  videoUsable(callback) {
    localStorage.setItem('videoStatus', 1)
    this.runCallback(callback)
  },
  audioUsable(callback) {
    localStorage.setItem('audioStatus', 1)
    this.runCallback(callback)
  },
  runCallback(callback) {
    //确保callback是一个函数
    if (typeof callback === 'function') {
      callback()
    }
  }
}
aiMediaCheck.initSet()
export default aiMediaCheck
